package com.qf.swagger2202.controller;

import com.qf.swagger2202.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/hello")
public class HelloController {


    @GetMapping("/test1")
    public Map<String,Object> test1(String name, Integer age){
        System.out.println(name);
        System.out.println(age);
        final HashMap<String, Object> map = new HashMap<>();

        map.put("code",1);
        map.put("msg","hello");

        return map;
    }


    @RequestMapping(value = "/test2",method = RequestMethod.POST)
    public User getUser(){
        System.out.println("getUser....");
        User user = new User();
        user.setName("jack");
        user.setPassword("123");
        user.setId(100);
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        user.setSex("1");
        return user;
    }


    /**
     * 根据用户id查询指定用户数据
     */
    @ApiOperation("根据用户id查询指定用户数据")
    @GetMapping("/getUser/{id}")
    public User findById(@PathVariable(value = "id") Long id) {
        System.out.println(id);
        User user = new User();
        user.setId(1);
        user.setPassword("123");
        user.setName("张三");
        user.setRegistTime(new Date());
        user.setBirthday(new Date());
        user.setSex("1");
        return user;
    }


    /**
     * 查询所有用户数据返回
     */
    @ApiOperation("查询所有用户数据返回")
    @GetMapping("/findAll")
    public List<User> findUserAll() {
        //1. 创建用户集合对象
        List<User> userList = new ArrayList<>();

        User user = new User();
        user.setId(1);
        user.setPassword("123");
        user.setName("张三");
        user.setRegistTime(new Date());
        user.setBirthday(new Date());
        user.setSex("1");

        userList.add(user);
        userList.add(user);

        //2. 返回用户集合对象
        return userList;
    }


    /**
     * 添加新用户
     * @param user
     */
    @ApiOperation("添加新用户")
    @PostMapping("/add")
    public User addUser(@RequestBody User user) {
        System.out.println("====" + user);
        System.out.println("=====添加用户成功=====");
        user.setId(user.getId()+1);
        return user;
    }


    /**
     * 根据用户主键id修改用户属性
     * @param user
     */
    @ApiOperation("根据id修改用户属性")
    @PutMapping("/edit")
    public User editUser(@RequestBody User user) {
        System.out.println("====" + user);
        System.out.println("=====修改用户成功=====");
        return user;
    }

    /**
     * 根据id删除用户
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/del/{id}")
    public Long delbyId(@PathVariable(value = "id") Long id) {
        System.out.println("======删除用户成功=====");
        return id;
    }

    @RequestMapping(value = "/delusers",method = RequestMethod.POST)
    @ApiOperation("根据id数组删除用户")
    public Integer deleteUsers( Integer[] ids ){
        List<Integer> idList = Arrays.asList(ids);
        idList.forEach(System.out::println);
        System.out.println("=====删除用户成功=====");
        return 0 ;
    }
}
